{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Robodyno LED Sensor API"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.初始化LED对象"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`LedDriver(can, id_)`\n",
    "\n",
    "参数：\n",
    "- `can` : can总线接口\n",
    "- `id_` : LED驱动模块ID，默认`0x33`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from robodyno.interfaces import CanBus\n",
    "from robodyno.components import LedDriver\n",
    "\n",
    "can_bus = CanBus()\n",
    "strip = LedDriver(can_bus, 0x33)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.设置CAN_ID"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`config_can_bus(new_id=None, bitrate = 1000000)`\n",
    "\n",
    "参数 ：\n",
    "\n",
    "- `new_id` ：LED 灯带新CAN_ID (默认：0x33)\n",
    "- `bitrate` : CAN总线通讯速率 (默认：1000000)\n",
    "    - 1000000\n",
    "    - 500000\n",
    "    - 250000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "strip.config_can_bus(0x33)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.设置LED颜色"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`set_color(id_, color)`\n",
    "\n",
    "参数 ：\n",
    "\n",
    "- `id_` ：LED的id\n",
    "  - `int` : 控制对应ID的LED的颜色\n",
    "  - `tuple` : 控制从元组中的第一个id开始的多个led的颜色，直到元组中的第二个id。\n",
    "- `color` : LED的颜色`（red, green, blue）`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "strip.set_color(0, (0, 255, 0)) # 设置第 0 个灯珠为绿色"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "strip.set_color((1, 3), (255, 0, 0)) # 设置第 1 ~ 3 个灯珠为红色"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.设置LED闪烁"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`blink(id_, color, period)`\n",
    "\n",
    "参数 ：\n",
    "\n",
    "- `id_` ：LED的id\n",
    "  - `int` : 控制对应ID的LED的颜色\n",
    "  - `tuple` : 控制从元组中的第一个id开始的多个led的颜色，直到元组中的第二个id。\n",
    "- `color` : LED的颜色`（red, green, blue）`\n",
    "- `period` : 闪烁周期的一半，单位为秒。led将在周期秒内亮，在周期秒内灭。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置第 0 个灯珠为闪烁模式，闪烁频率为 1 Hz，闪烁颜色为红色\n",
    "strip.blink(0, (255, 0, 0), 1.0 / 2)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.设置LED呼吸"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`breathe(id_, color, period)`\n",
    "\n",
    "参数 ：\n",
    "\n",
    "- `id_` ：LED的id\n",
    "  - `int` : 控制对应ID的LED的颜色\n",
    "  - `tuple` : 控制从元组中的第一个id开始的多个led的颜色，直到元组中的第二个id。\n",
    "- `color` : LED的颜色`（red, green, blue）`\n",
    "- `period` : 闪烁周期的一半，单位为秒。led将在周期秒内亮，在周期秒内灭。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置第 0 个灯珠为呼吸模式，呼吸频率为 1 Hz，呼吸颜色为红色\n",
    "strip.breathe(0, (255, 0, 0), 1.0 / 2)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6.设置LED跑马灯"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`marquee(id_, color, period)`\n",
    "\n",
    "参数 ：\n",
    "\n",
    "- `id_` ：LED的id\n",
    "  - `tuple` : 控制从元组中的第一个id开始的多个led的颜色，直到元组中的第二个id。\n",
    "- `color` : LED的颜色`（red, green, blue）`\n",
    "- `period` : 闪烁周期的一半，单位为秒。led将在周期秒内亮，在周期秒内灭。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置第 0 ~ 3 个灯珠为跑马灯模式，跑马灯频率为 1 Hz，跑马灯颜色为红色\n",
    "strip.marquee((0, 3), (255, 0, 0), 1.0 / 2)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7.关闭所有 LED"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`clear()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "strip.clear() # 关闭所有 LED"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
